<!-- 首页 -->
<template>
	<view class="main">
		<view class="head">
			<!-- <u-navbar :leftIcon="false" title="首页" bg-color="#FCB527" :border="false"></u-navbar> -->
		</view>
		<view class="content flex flex-dir-c">
			<view class="card flex flex-jc-sa flex-ai-c">
				<view class="card-text">
					<view class="flex flex-ai-c flex-jc-c">
						<i-icon icon="dingdan" />
						<view style="margin-left: 10rpx;">今日订单</view>
					</view>
					<view class="card-text-order">{{info.today_order_num}}</view>
				</view>
				<view class="card-text" style="border-left: 4rpx solid #F1F1F1;border-right: 4rpx solid #F1F1F1;">
					<view class="flex flex-ai-c flex-jc-c">
						<i-icon icon="shouyimingxi" />
						<view style="margin-left: 10rpx;">今日收益</view>
					</view>
					<view class="card-text-order" style="color: #666666;">{{info.today_income}}</view>
				</view>
				<view class="card-text">
					<view class="flex flex-ai-c flex-jc-c">
						<i-icon icon="xiangmushouru" />
						<view style="margin-left: 10rpx;">昨日收益</view>
					</view>
					<view class="card-text-order" style="color: #666666;">{{info.yestday_income}}</view>
				</view>
			</view>
			<view class="list f1">
				<view style="padding: 30rpx;">我的设备</view>
				<view v-for="item in list" :key="item.id" @click="skip(item)">
					<view class="flex"
						style="background: #fff;padding: 25rpx 20rpx;border-radius: 10rpx;margin-bottom: 20rpx;">
						<view style="margin-right: 10rpx;">
							<u-image width="112rpx" height="112rpx"
								:src="item.equipment_draw.length ? item.equipment_draw[0]:''"></u-image>
						</view>
						<view class="f1">
							<view class="flex flex-jc-sb">
								<view class="list-title description">{{item.device_name}}</view>
								<view class="type" :class="'type-' + item.status">{{item.status | filtetState}}</view>
							</view>
							<view class="list-address">{{item.addr}}</view>
							<view class="list-address">{{item.distance_text}}</view>
						</view>
					</view>
				</view>
				<view class="flex flex-jc-c flex-ai-c" v-if="!list.length" style="height: 50vh;">
					<u-empty text="数据为空" mode="list"></u-empty>
				</view>
			</view>
		</view>
		<TabBar :current-page="0" />
	</view>
</template>

<script>
	import home from '@/api/home.js'
	import getLocationAuth from '@/utils/getLocationAuth.js';
	import subscribeMessage from '@/utils/subscribeMessage.js';
	import {
		fail
	} from 'assert';
	export default {
		data() {
			return {
				list: [],
				info: {
					today_order_num: 0,
					today_income: 0,
					yestday_income: 0
				},
			}
		},
		onLoad() {
			if(!uni.getStorageSync('token')) return
			//#ifdef H5
			this.home()
			//#endif
			// #ifndef H5
			this.location()
			// #endif
		},
		filters: {
			filtetState(val) {
				let text = ''
				switch (val) {
					case 0:
						text = '离线'
						break
					case 1:
						text = '在线'
						break
					case 2:
						text = '故障'
						break
				}
				return text
			}

		},

		methods: {
			setAuth(res) {
				if (res.subscriptionsSetting.mainSwitch) {
					wx.showModal({
						title: '提示',
						content: '授权开通订阅消息通知',
						showCancel: true,
						success: function(ress) {
							if (ress.confirm) {
								wx.requestSubscribeMessage({ // 调起消息订阅界面
									tmplIds: subscribeMessage.auth,
									success(res) {
										console.log('订阅消息 成功 ');
										uni.setStorageSync('isLogin', false)
									},
									fail(er) {
										console.log("订阅消息 失败 ");
									}
								})
							}
						}
					})
				} else {
					wx.showModal({
						content: '检测到您没打开消息推送权限，是否去设置打开？',
						success: function(res) {
							if (res.confirm) {
								wx.openSetting()
							}
						},
					});
				}
			},
			//获取消息订阅权限
			getMessgeAuth() {
				let _this = this
				wx.getSetting({
					withSubscriptions: true,
					success: res => {
						// 判断是否已经授权
						_this.setAuth(res)
					},
					fail: () => {
						uni.setStorageSync('isLogin', false)
					}
				})
			},
			async wxAuth(encryptedData, iv, code) {
				const res = home.wxAuth({
					code: encodeURI(code),
					encryptedData: encodeURI(encryptedData),
					iv: encodeURI(iv)
				}).then(res => {
					this.getMessgeAuth()
				})
			},
			getSetting(code) {
				let _this = this
				wx.getSetting({
					success(res3) {
						if (!res3.authSetting['scope.userInfo']) {
							wx.authorize({
								scope: 'scope.userInfo',
								success() {
									//发起网络请求
									wx.getUserInfo({
										success: function(
											res2
										) {
											const {
												encryptedData,
												iv
											} = res2
											_this.wxAuth(encryptedData, iv, code)
										}
									})
								}
							})
						} else {
							wx.getUserInfo({
								success: function(
									res2
								) {
									const {
										encryptedData,
										iv
									} = res2
									_this.wxAuth(encryptedData, iv, code)
								}
							})
						}
					}
				})

			},
			wxlogin() {
				let _this = this
				wx.showModal({
					title: '是否授权微信登录?',
					content: '',
					complete: (res) => {
						if (res.confirm) {
							wx.login({
								success: (res) => {
									if (res.code) {
										_this.getSetting(res.code)
									}
								}
							})
						}
					}
				})
			},
			onPullDownRefresh() {
				this.location()
			},
			location() {
				uni.showLoading({
					title: '获取定位中'
				})
				uni.getLocation({
					type: 'gcj02',
					geocode: true,
					success: res => {
						uni.setStorageSync('address', {
							lat: res.latitude,
							lng: res.longitude
						})
						this.home(res.latitude, res.longitude)
						uni.stopPullDownRefresh()
						uni.hideLoading()
					},
					fail: () => {
						uni.setStorageSync('address', {
							lat: 37.870472,
							lng: 112.548877
						})
						this.home()
						uni.stopPullDownRefresh()
						uni.hideLoading()
						//#ifdef APP-PLUS
						getLocationAuth() // 获取系统定位权限
						//#endif
						//#ifdef MP-WEIXIN
						wx.getSetting({
							success: (res) => {
								if (!res.authSetting['scope.userLocation']) {
									wx.showModal({
										content: '检测到您没打开定位权限，是否去设置打开？',
										success: function(res) {
											if (res.confirm) {
												wx.openSetting()
											}
										},
									});
								} else {
									uni.showToast({
										title: '检测到您没打开定位开关',
										icon: 'none'
									})
								}
							},

						})
						//#endif
					}
				})


			},
			async home(lat = 0, lng = 0) {
				let addr = uni.getStorageSync('address')
				lat = lat ? lat : addr.lat
				lng = lng ? lng : addr.lng
				const res = await home.getHome({
					lng: lng || 0,
					lat: lat || 0
				})
				if (res.code == 200) {
					this.info = res.data.chart
					this.list = res.data.device
					if(uni.getStorageSync('isLogin')){
						this.wxlogin()
					}
				}

			},
			skip(item){
				uni.navigateTo({
					url: `/pages/equipment/preview?device_no=${item.device_no}`
				});
			},

		}
	}
</script>
<style>
	page {
		background: #F2F2F2;
		height: 100%;
	}
</style>
<style scoped lang="scss">
	.type-1 {
		background: #30D158;
	}

	.type-0 {
		background: #999999;
	}

	.type-2 {
		background: #F95533;
	}

	.type {
		border-radius: 5rpx;
		padding: 4rpx 10rpx;
		font-size: 22rpx;
		font-weight: 500;
		color: #FFFFFF;
	}

	.description {
		display: -webkit-box;
		overflow: hidden;
		text-overflow: ellipsis;
		-webkit-line-clamp: 1;
		-webkit-box-orient: vertical;
	}

	.main {
		height: 100%;

		position: relative;

		.head {
			height: 150rpx;
			background: #FCB527;
		}

		.content {

			padding: 0 30rpx;

			.card {

				height: 196rpx;
				background: #FFFFFF;
				border-radius: 30rpx;
				position: relative;
				top: -70rpx;

				.card-text {
					width: 33.3%;
					text-align: center;
					font-size: 28rpx;

					.card-text-order {

						font-size: 48rpx;
						font-weight: 500;
						color: #FCB527;
					}
				}
			}

			.list {
				position: relative;
				top: -70rpx;

				.list-title {
					font-size: 30rpx;
					font-weight: 500;
					color: #000000;
				}

				.list-address {
					font-size: 24rpx;
					font-weight: 400;
					color: #666666;
				}
			}
		}
	}
</style>